﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageAdmin
{
    public partial class RoleEdit : UserControl
    {
        private static AdminLogic adminBll;
        public V_Admin adminView;
        public T_Admin admin;
        public AdminList adminList;
        public TreeLogic LgcTree;
        public T_Tree Tree;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public RoleEdit()
        {
            adminBll = new AdminLogic();
            adminView = new V_Admin();
            admin = new T_Admin();
            LgcTree = new TreeLogic();
            Tree = new T_Tree();
            InitializeComponent();

        }

        public void GetRole(string id)
        {
            Tree.Id = id;//点修改进来的时候给Id赋值,用于判断是添加还是修改

            string whereStr = string.Format(" where Id='{0}'", id);
            Tree = adminBll.GetRoles(whereStr).FirstOrDefault();
            if (Tree == null) return;
            txtName.Text = Tree.Name;
            tvPower.Nodes.Clear(); 
            var adminPowerMapps = adminBll.GetAdminPowerMapps(" AdminId = '" + Tree.Id + "'").ToList();
            LgcTree.SetAdminPowerMappTreeNode(LgcTree.GetTreeByType("AdminPower"), tvPower, adminPowerMapps);

        }

        public void SetRole(T_Tree tree)
        {
            Tree = tree;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var adminPowerMapps = new List<T_AdminPowerMapp>();
            SPACommon.CheckAllChildNodes(tvPower.TopNode, true, adminPowerMapps);

            if (CheckInput())
            {
                Boolean add = true;
                SetValue(Tree);
                pbClose.Enabled = false;
                btnCancel.Enabled = false;
                if (!string.IsNullOrEmpty(Tree.Id)) add = false;
                adminBll.SaveRole(Tree, adminPowerMapps);
                pbClose.Enabled = true;
                btnCancel.Enabled = true;
                //AdminList.Instance.LoadGridView();
                if (add) AdminList.Instance.AddTree(Tree);
                else AdminList.Instance.UpdateTree(Tree);
                this.FindForm().Close();
            }
        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请输入人员名称！");
                return false;
            }

            return true;
        }


        public void SetValue(T_Tree tree)
        {
            tree.Name = txtName.Text.Trim();
            //tree.AddTime = DateTime.Now;
        }

        private void btnRole_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("AdminTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;

            admin.TreeId = formTreeSelecter.TypeId;
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void tvPower_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                foreach (TreeNode node in e.Node.Nodes)
                {
                    node.Checked = e.Node.Checked;
                }
            }

        }

        private void AdminAdd_Load(object sender, EventArgs e)
        {
            if (tvPower.Nodes.Count > 0) return;
            LgcTree.SetTreeNode(LgcTree.GetTreeByType("AdminPower"), tvPower);
            tvPower.ExpandAll();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            SPACommon.ExportToExcel(tvPower,"权限");
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
    }
}
